//package ui.forms;
//
//import base.Intake;
//import db.IntakeManager;
//import utils.DateUtils;
//
//import com.sun.lwuit.*;
//import com.sun.lwuit.events.*;
//import com.sun.lwuit.layouts.*;
//import com.sun.lwuit.list.DefaultListCellRenderer;
//import java.util.*;
//import ui.YearMonthSelector;
//
///**
// * Displays the intake information of the user on a weekly basis for a particular month
// *
// * @author Meera
// */
//public class ViewReportForm extends Form implements ActionListener, DataChangedListener {
//
//    private Button btnWeek1;
//    private Button btnWeek2;
//    private Button btnWeek3;
//    private Button btnWeek4;
//    private Button btnWeek5;
//    private Label wkTotal;
//    private Label mtTotal;
//    private TextField txtWeekTotal;
//    private TextField txtMonthTotal;
//    private List lstWeekDays;
//    private List lstDiet;
//    private String[] weekDays = new String[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
//    private int selectedWeenNo = 1;
//    private Label lblTotal;
//    private Container cntList;
//    private Container cntBottom;
//    String selDate = new String();
//    private Command cmdBack;
//    private YearMonthSelector yearMonthSelector;
//
//    public ViewReportForm(final Form parent) {
//        cmdBack = new Command("Back") {
//
//            public void actionPerformed(ActionEvent evt) {
//                parent.showBack();
//            }
//        };
//        addCommand(cmdBack);
//        setBackCommand(cmdBack);
//
//        setLayout(new BorderLayout());
//
//        lstWeekDays = new List();
//
//        lstWeekDays.setListCellRenderer(new DefaultListCellRenderer(false));
//        for (int i = 0; i < weekDays.length; i++) {
//            lstWeekDays.addItem(weekDays[i]);
//        }
//        lstWeekDays.setPreferredW(getPreferredW() / 5);
//        lstWeekDays.setPreferredH(getPreferredH() / 3);
//        // lstTitle.setSelectedIndex(0);
//
//        lstDiet = new List();
//        lstDiet.setSmoothScrolling(true);
//        lstDiet.getStyle().setBorder(null);
//        lstDiet.setListCellRenderer(new ui.DietListRenderer());
//        lstDiet.setPreferredH(getPreferredH() / 3);
//        lstDiet.setPreferredW(getPreferredW());
//
//        lblTotal = new Label("Total:");
//        wkTotal = new Label("Week");
//        txtWeekTotal = new TextField("", 3);
//        txtWeekTotal.setEditable(false);
//
//        mtTotal = new Label("Month");
//        txtMonthTotal = new TextField("", 3);
//        txtMonthTotal.setEditable(false);
//
//        btnWeek1 = new Button("WK1");
//        btnWeek2 = new Button("WK2");
//        btnWeek3 = new Button("WK3");
//        btnWeek4 = new Button("WK4");
//        btnWeek5 = new Button("WK5");
//        btnWeek1.addActionListener(this);
//        btnWeek2.addActionListener(this);
//        btnWeek3.addActionListener(this);
//        btnWeek4.addActionListener(this);
//        btnWeek5.addActionListener(this);
//
//        GridLayout weekGrid = new GridLayout(1, 5);
//        Container cntWeeks = new Container(weekGrid);
//
//        cntWeeks.addComponent(0, btnWeek1);
//        cntWeeks.addComponent(1, btnWeek2);
//        cntWeeks.addComponent(2, btnWeek3);
//        cntWeeks.addComponent(3, btnWeek4);
//        cntWeeks.addComponent(4, btnWeek5);
//
//        yearMonthSelector = new YearMonthSelector();
//        yearMonthSelector.setDataChangedListener(this);
//
//        Container cntTop = new Container(new BorderLayout());
//        cntTop.addComponent(BorderLayout.NORTH, yearMonthSelector);
//        cntTop.addComponent(BorderLayout.SOUTH, cntWeeks);
//
//        cntBottom = new Container(new BoxLayout(BoxLayout.X_AXIS));
//        cntBottom.addComponent(lblTotal);
//        cntBottom.addComponent(wkTotal);
//        cntBottom.addComponent(txtWeekTotal);
//        cntBottom.addComponent(mtTotal);
//        cntBottom.addComponent(txtMonthTotal);
//
//        cntList = new Container(new BoxLayout(BoxLayout.X_AXIS));
//        cntList.addComponent(lstWeekDays);
//        cntList.addComponent(lstDiet);
//
//        addComponent(BorderLayout.NORTH, cntTop);
//        addComponent(BorderLayout.CENTER, cntList);
//        addComponent(BorderLayout.SOUTH, cntBottom);
//        addCommandListener(this);
//
//        lstWeekDays.getModel().addSelectionListener(new SelectionListener() {
//
//            private void showReportForm(int curSelIndex) {
//                if (((lstDiet.getModel().getItemAt(curSelIndex)) != " ")) {
//                    selDate = DateUtils.getDaysOfWk(
//                            curSelIndex,
//                            selectedWeenNo,
//                            String.valueOf(yearMonthSelector.getMonth() + 1),
//                            String.valueOf(yearMonthSelector.getYear()));
//                    DetailedReportForm frmReport = new DetailedReportForm(ViewReportForm.this, selDate);
//                    frmReport.show();
//                }
//            }
//
//            public void selectionChanged(int oldSelIndex, int curSelIndex) {
//                try {
//                    if (oldSelIndex != curSelIndex) {
//                        if (((lstDiet.getModel().getItemAt(curSelIndex)) != null)) {
//                            showReportForm(curSelIndex);
//                        } else {
//                            Dialog.show("Error", "No data selected", "OK", "");
//                        }
//                    }
//
//                } catch (Exception ex) {
//                    ex.printStackTrace();
//                    Dialog.show("Exception", ex.toString(), "OK", null);
//                }
//            }
//        });
//
//        updateList(1);
//    }
//
//    public int getMonthlyCalories(String frmDate, String toDate) {
//        try {
//            Vector result = IntakeManager.getIntakes(
//                    Context.currentUser.getId(),
//                    DateUtils.stringToDate(frmDate),
//                    DateUtils.stringToDate(toDate));
//            int mthlyCal = 0;
//            for (int j = 0; j < (result.size()); j++) {
//                Intake intake = (Intake) result.elementAt(j);
//                mthlyCal += (int) intake.getCalories();
//            }
//            return mthlyCal;
//        } catch (Exception ex) {
//            ex.printStackTrace();
//            Dialog.show("Exception", ex.toString(), "OK", null);
//        }
//        return 0;
//    }
//
//    java.util.Calendar cal = java.util.Calendar.getInstance();
//
//    private void fillDietList(Date startDate, Date endDate) {
//        try {
//            for (int i = lstDiet.size(); i >= 0; i--) {
//                lstDiet.getModel().removeItem(i);
//            }
//            lstDiet.repaint();
//            Vector result = IntakeManager.getIntakes(
//                    Context.currentUser.getId(),
//                    startDate,
//                    endDate);
//
//            String[] fd = new String[7];
//
//            cal.setTime(startDate);
//            int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK);
//            int dailyCalories = 0;
//            int weeklyCalories = 0;
//            for (int k = 1; k < dayOfWeek; k++) {
//                lstDiet.addItem(" ");
//            }
//            Date day = startDate;
//            for (int i = 0; i <= (7 - dayOfWeek); i++) {
//                fd[i] = "";
//                for (int j = 0; j < (result.size()); j++) {
//                    try {
//                        Intake intake = (Intake) result.elementAt(j);
//
//                        long intakeTime = intake.getDate().getTime();
//                        long startOfDay = DateUtils.getStartOfDay(day).getTime();
//                        long endOfDay = DateUtils.getEndOfDay(day).getTime();
//
//                        if (intakeTime >= startOfDay && intakeTime < endOfDay) {
//                            fd[i] += intake.getFood().getName() + ",";
//                            dailyCalories += (int) (intake.getCalories());
//                        }
//                    } catch (Exception ex) {
//                        ex.printStackTrace();
//                        Dialog.show("Exception", ex.getMessage(), "OK", null);
//                    }
//                }
//                fd[i] = Integer.toString(dailyCalories) + " kCal :" + fd[i];
//                weeklyCalories = weeklyCalories + dailyCalories;
//                lstDiet.addItem(fd[i]);
//                day = DateUtils.getNextDay(day);
//                dailyCalories = 0;
//            }
//            lstDiet.repaint();
//            txtWeekTotal.setText(Integer.toString(weeklyCalories));
//            txtWeekTotal.repaint();
//        } catch (Exception ex) {
//            ex.printStackTrace();
//            Dialog.show("Error", ex.toString(), "OK", "");
//        }
//    }
//
//    public void actionPerformed(ActionEvent evt) {
//        if (evt.getSource() == btnWeek1) {
//            selectedWeenNo = 1;
//            updateList(1);
//        }
//        if (evt.getSource() == btnWeek2) {
//            selectedWeenNo = 2;
//            updateList(2);
//        }
//        if (evt.getSource() == btnWeek3) {
//            selectedWeenNo = 3;
//            updateList(3);
//        }
//        if (evt.getSource() == btnWeek4) {
//            selectedWeenNo = 4;
//            updateList(4);
//        }
//        if (evt.getSource() == btnWeek5) {
//            selectedWeenNo = 5;
//            updateList(5);
//        }
//    }
//
//    private void updateList(int weekNo) {
//        for (int i = lstDiet.size(); i >= 0; i--) {
//            lstDiet.getModel().removeItem(i);
//        }
//        lstDiet.repaint();
//        cntList.removeComponent(lstDiet);
//        removeComponent(cntList);
//        removeComponent(cntBottom);
//        String[] weekDate = DateUtils.getWeekDates(
//                String.valueOf(yearMonthSelector.getYear()),
//                String.valueOf(yearMonthSelector.getMonth() + 1));
//        fillDietList(
//                DateUtils.stringToDate(weekDate[weekNo * 2 - 2]),
//                DateUtils.stringToDate(weekDate[weekNo * 2 - 1]));
//        // Week 1-> 0 ,1
//        // Week 2-> 2, 3
//        // Week 3-> 4, 5
//        // Week 4-> 6, 7
//        // Week 5-> 8, 9
//        lstDiet.repaint();
//        cntList.addComponent(1, lstDiet);
//        cntList.repaint();
//        cntBottom.repaint();
//        addComponent(BorderLayout.CENTER, cntList);
//        addComponent(BorderLayout.SOUTH, cntBottom);
//        txtMonthTotal.setText(Integer.toString(this.getMonthlyCalories(weekDate[0], weekDate[9])));
//        repaint();
//    }
//
//    public void dataChanged(int type, int index) {
//        selectedWeenNo = 1;
//        updateList(selectedWeenNo);
//    }
//}
